<?php


namespace App\Services;


use App\Repositories\PatentRepository;
use Encore\Admin\Widgets\Box;
use Illuminate\Support\Str;

class StatisticServer
{
    protected $patentRepository;

    public function __construct(PatentRepository $patentRepository)
    {
        $this->patentRepository = $patentRepository;
    }

    public function patent($user, $role)
    {
        if (!config('settings.module.patent_module')) {
            return '';
        }
        $patent_types = $this->patentRepository->typeStatistic($user, $role);
        $types = [
            '发明专利' => $patent_types[1] ?? 0,
            '实用新型' => $patent_types[2] ?? 0,
            '外观设计' => $patent_types[3] ?? 0
        ];
        $dates = $this->patentRepository->applyDateStatistic($user, $role);
        $goods = $this->patentRepository->legalStatusStatistic($user, $role);
        $list = [
            [
                'type' => 'patent_legal',
                'name' => '专利法律状态',
                'data' => $goods
            ], [
                'type' => 'patent_type',
                'name' => '专利类型数量',
                'data' => $types
            ], [
                'type' => 'patent_apply_date',
                'name' => '近10年年申请数量',
                'data' => $dates->map(function ($value, $key) {
                    return ['key' => $key . '年', 'value' => $value];
                })->pluck('value', 'key')
            ],
        ];
        return $this->pie3Chart('专利统计', $list);
    }

    public function patent2()
    {
        if (!config('settings.module.patent_module')) {
            return '';
        }
        $x = [];
        $year = date('Y');
        $first_year = $year - 4;
        for ($i = $first_year; $i <= $year; $i++) {
            $x[] = $i;
        }
        $collect = $this->patentRepository->applyShouQuanStatistic($first_year);
        $list = [
            [
                'id'    => Str::random(32),
                'title' => '近5年受理数量',
                'data'  => collect($x)->map(function ($item) use ($collect) {
                    $total = $collect->where('a_year', $item)->sum('count') ?: 0;
                    $count = $collect->where('a_year', $item)->where('patent_type_id', 1)->sum('count') ?: 0;
                    return collect([
                        "'{$item}年'",
                        $count,
                        $total - $count,
                    ])->implode(",");
                })->prepend("'patent','发明','其它'"),
            ], [
                'id'    => Str::random(32),
                'title' => '近5年授权数量',
                'data'  => collect($x)->map(function ($item) use ($collect) {
                    $total = $collect->where('p_year', $item)->sum('count') ?: 0;
                    $count = $collect->where('p_year', $item)->where('patent_type_id', 1)->sum('count') ?: 0;
                    return collect([
                        "'{$item}年'",
                        $count,
                        $total - $count,
                    ])->implode(",");
                })->prepend("'patent','发明','其它'"),
            ]
        ];
        return $this->barChart('专利统计', $list);
    }

    protected function shouQuanData($year_num)
    {
        $x = [];
        $year = date('Y');
        for ($i = $year_num; $i > 0; $i--) {
            $x[] = $year - $i + 1;
        }
        $collect = $this->patentRepository->shouQuanStatistic($year_num);
        $typeOptions = patent_type_select_option();
        $data = collect($typeOptions)->map(function ($name, $id) use ($collect, $x) {
            $arr = $collect->where('patent_type_id', $id);
            return [
                'name' => $name,
                'data' => collect($x)->map(function ($year) use ($arr) {
                    return $arr->where('year', $year)->sum('count') ?: 0;
                })->implode(',')
            ];
        });
        return [
            'id'     => Str::random(32),
            'title'  => '近年授权数量',
            'years'  => $x,
            'legend' => $typeOptions,
            'data'   => $data
        ];
    }


    protected function pie3Chart($title, $list): Box
    {
        return (new Box($title, view('admin.chartjs.pie3-tj', compact('list'))))
            ->style('primary')
            ->solid();
    }

    protected function lineChart($title, $list): Box
    {
        return (new Box($title, view('admin.chartjs.line-tj', compact('list'))))
            ->style('primary')
            ->solid();
    }

    protected function barChart($title, $list): Box
    {
        return (new Box($title, view('admin.chartjs.bar-tj', compact('list'))))
            ->style('primary')
            ->solid();
    }

}
